const path = require('path')
const webpack = require('webpack')

const config = {
  parser: require('postcss-comment'),
  plugins: [
    require('postcss-import')({
      resolve(id, basedir, importOptions) {
        if (id.startsWith('~@/')) {
          return path.resolve(process.env.UNI_INPUT_DIR, id.substr(3))
        } else if (id.startsWith('@/')) {
          return path.resolve(process.env.UNI_INPUT_DIR, id.substr(2))
        } else if (id.startsWith('/') && !id.startsWith('//')) {
          return path.resolve(process.env.UNI_INPUT_DIR, id.substr(1))
        }
        return id
      },
    }),
    require('autoprefixer')({
      remove: process.env.UNI_PLATFORM !== 'h5',
    }),
    require('@dcloudio/vue-cli-plugin-uni/packages/postcss'),
    // 借助postcss-px-to-viewport插件，实现px转rpx，文档：https://github.com/evrone/postcss-px-to-viewport/blob/master/README_CN.md
    // 以下配置，可以将px转换为2倍的rpx，如10px=20rpx，如果要调整比例，可以调整 viewportWidth 来实现
    require('postcss-px-to-viewport')({
      unitToConvert: 'px',
      viewportWidth: 50, // 计算公式：y = (x / viewportWidth) * 100
      unitPrecision: 5,
      propList: ['*'],
      viewportUnit: 'rpx',
      fontViewportUnit: 'rpx',
      selectorBlackList: ['.ze-tabbar'],
      minPixelValue: 1,
      mediaQuery: false,
      replace: true,
      exclude: undefined,
      include: undefined,
      landscape: false,
    }),
  ],
}

if (webpack.version[0] > 4) {
  delete config.parser
}

module.exports = config
